# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

from selenium.webdriver.common.by import By
from selenium import webdriver
import time


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    #打开一个Chrome浏览器，打开百度首页
    driver = webdriver.Chrome()
    driver.get("https://www.baidu.com")

    #窗口最大化
    driver.maximize_window()

    #通过id定位
    element = driver.find_element(By.ID, "kw")
    if (element):
        print("Find element by Id successfully!")

    #通过name定位
    element = driver.find_element(By.NAME, "wd")
    if (element):
        print("Find element by name successfully!")

    #通过Xpath定位
    #相对路径xpath定位
    element = driver.find_element(By.XPATH, "//*[@id='kw']")
    if (element):
        print("Find element by xpath successfully!")
    #绝对路径xpath定位
    #element = driver.find_element(By.XPATH, "/html/body/div[2]/div[1]/div[5]/div/div/form/span[1]/input")
    #if (element):
    #    print("Find element by full-xpath successfully!")

    #通过CSS选择器定位
    element = driver.find_element(By.CSS_SELECTOR, "#kw") #id定位
    #element = driver.find_element(By.CSS_SELECTOR, "[name=wd]") #name属性值定位
    #element = driver.find_element(By.CSS_SELECTOR, ".s_ipt") #class定位
    #element = driver.find_element(By.CSS_SELECTOR, "html > body > form > span > input") #CSS层级定位
    #element = driver.find_element(By.CSS_SELECTOR, "span.soutu-btn > input#kw") #组合定位
    if (element):
        print("Find element by CSS selector successfully!")

    #输入文本
    element.send_keys("春游")
    time.sleep(3)
    element.clear()
    element.send_keys("上班")

    #左键点击元素
    element_btn = driver.find_element(By.CLASS_NAME, "s_btn")
    if (element_btn.click()):
       print("Click search button successfully!")

